package com.lzb.permanentcalendar.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.TimeUtils;
import com.lzb.permanentcalendar.BaseNoModelFragment;
import com.lzb.permanentcalendar.R;
import com.lzb.permanentcalendar.databinding.FragmentDateIntervalBinding;
import com.lzb.permanentcalendar.dialog.SelDateDialog;
import com.lzb.permanentcalendar.utils.DateCalculator;
import com.nlf.calendar.Lunar;
import com.nlf.calendar.Solar;

import java.text.SimpleDateFormat;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link DateIntervalFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class DateIntervalFragment extends BaseNoModelFragment<FragmentDateIntervalBinding> implements View.OnClickListener {

    private SelDateDialog firstDateDialog, secondDateDialog;
    private long startDate, endDate;

    @Override
    protected int onCreate() {
        return R.layout.fragment_date_interval;
    }

    @Override
    protected void initView() {
        mDataBinding.llFirstDate.setOnClickListener(this);
        mDataBinding.llSecondDate.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        startDate = TimeUtils.getNowMills();
        endDate = TimeUtils.getNowMills();
        mDataBinding.tvFirstDate.setText(TimeUtils.millis2String(startDate, new SimpleDateFormat("yyyy-MM-dd")));
        mDataBinding.tvSecondDate.setText(TimeUtils.millis2String(endDate, new SimpleDateFormat("yyyy-MM-dd")));
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.llFirstDate) {//第一个日期
            showFirstDateDialog();
        } else if (id == R.id.llSecondDate) {//第二个日期
            showSecondDateDialog();
        }
    }

    private void showFirstDateDialog() {
        if (firstDateDialog == null) firstDateDialog = new SelDateDialog(mContext);
        firstDateDialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void clickConfirm(int type, int year, int mo, int days) {
                if (type == 0) {
                    String date = year + "-" + (mo < 10 ? "0" + mo : mo) + "-" + (days < 10 ? "0" + days : days);
                    startDate = TimeUtils.string2Millis(date, new SimpleDateFormat("yyyy-MM-dd"));
                    mDataBinding.tvFirstDate.setText(date);
                } else if (type == 1) {
                    Lunar lunar = new Lunar(year, mo, days);
                    // 转换为阳历
                    Solar solar = lunar.getSolar();
                    String date = solar.getYear() + "-" + (solar.getMonth() < 10 ? "0" + solar.getMonth() : solar.getMonth()) + "-" + (solar.getDay() < 10 ? "0" + solar.getDay() : solar.getDay());
                    startDate = TimeUtils.string2Millis(date, new SimpleDateFormat("yyyy-MM-dd"));
                    mDataBinding.tvFirstDate.setText(date);
                }
                int num = Math.abs(DateCalculator.getDaysBetweenExact(startDate, endDate));
                mDataBinding.tvInterval.setText(num + "天");
            }
        });
        firstDateDialog.show();
    }

    private void showSecondDateDialog() {
        if (secondDateDialog == null) secondDateDialog = new SelDateDialog(mContext);
        secondDateDialog.setListener(new SelDateDialog.Ilistener() {
            @Override
            public void clickConfirm(int type, int year, int mo, int days) {
                if (type == 0) {
                    String date = year + "-" + (mo < 10 ? "0" + mo : mo) + "-" + (days < 10 ? "0" + days : days);
                    endDate = TimeUtils.string2Millis(date, new SimpleDateFormat("yyyy-MM-dd"));
                    mDataBinding.tvSecondDate.setText(date);
                } else if (type == 1) {
                    Lunar lunar = new Lunar(year, mo, days);
                    // 转换为阳历
                    Solar solar = lunar.getSolar();
                    String date = solar.getYear() + "-" + (solar.getMonth() < 10 ? "0" + solar.getMonth() : solar.getMonth()) + "-" + (solar.getDay() < 10 ? "0" + solar.getDay() : solar.getDay());
                    endDate = TimeUtils.string2Millis(date, new SimpleDateFormat("yyyy-MM-dd"));
                    mDataBinding.tvSecondDate.setText(date);
                }
                int num = Math.abs(DateCalculator.getDaysBetweenExact(startDate, endDate));
                mDataBinding.tvInterval.setText(num + "天");
            }
        });
        secondDateDialog.show();
    }

}